package com.community.web.master.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.community.model.entity.Goods;
import com.community.model.entity.Parking;
import com.community.model.entity.ParkingBind;
import com.community.web.master.domain.dto.PageParkingDto;
import com.community.web.master.domain.vo.GoodsWithCategoryNameVo;
import com.community.web.master.domain.vo.PageResult;
import com.community.web.master.domain.vo.ParkingWithBindVo;
import com.community.web.master.exception.DataException;
import com.community.web.master.mapper.ParkingBindMapper;
import com.community.web.master.mapper.ParkingMapper;
import com.community.web.master.service.IParkingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 李晶
 * @since 2024-07-02
 */
@Service
public class ParkingServiceImpl extends ServiceImpl<ParkingMapper, Parking> implements IParkingService {
    @Autowired
    private ParkingMapper parkingMapper;
    @Autowired
    private ParkingBindMapper parkingBindMapper;

    @Override
    public PageResult pageQuery(PageParkingDto pageParkingDto) {
        QueryWrapper<Parking> queryWrapper= new QueryWrapper<Parking>();
        if(pageParkingDto.getParkingType() !=null){
            queryWrapper.eq("parking_type",pageParkingDto.getParkingType());
        }
        Long total = parkingMapper.selectCount(queryWrapper);

        //多表联查,这里给查两个表User,parking
        Map<String,Object> map=new HashMap<>();
        map.put("limit",(pageParkingDto.getPageSize()));
        map.put("offset",(pageParkingDto.getPage()-1)*pageParkingDto.getPageSize());
        map.put("parking_type",pageParkingDto.getParkingType());
        List<ParkingWithBindVo> result= parkingMapper.selectPageByMap(map);
        //查到parking列表后,根据parking_id查询bind表中车牌,并且拼成字符串
        for (ParkingWithBindVo parkingWithBindVo : result) {
            QueryWrapper<ParkingBind> parkingBindQueryWrapper = new QueryWrapper<>();
            parkingBindQueryWrapper.eq("parking_id",parkingWithBindVo.getId());
            List<ParkingBind> parkingBinds = parkingBindMapper.selectList(parkingBindQueryWrapper);
            String s = parkingBinds.stream().map(ParkingBind::getLicensePlate).collect(Collectors.toList()).toString();

            parkingWithBindVo.setLicensePlate(s.substring(1,s.length()-1));
        }

        return new PageResult(total,result);
    }
}
